package com.my.mymafengwo.adapter;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.smallstrong.mafengwopro.R;
import com.my.mymafengwo.bean.ItemsEntity2;
import com.my.mymafengwo.bean.Store;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * ***********************************************
 * author:ljz
 * time:2015/12/8 15:55
 * name:
 * step:
 * ***********************************************
 */
    public class StoreThemesAdapter extends BaseAdapter {
    private static final int TYPE_SINGLE = 0;
    private static final int TYPE_MULTIPLE = 1;
    private static final int TYPE_COUNT = 2;

        private Context context;
        private LayoutInflater inflater;
        private List<Store.DataEntity.ThemesEntity> list;
        private List<Store.DataEntity.ThemesEntity.ItemsEntity> listItems;
        private List<ItemsEntity2> listItems2;
        private String result;

        public StoreThemesAdapter(Context context) {
            this.context = context;
            inflater = LayoutInflater.from(context);
            listItems=new ArrayList<>();
            listItems2=new ArrayList<>();
        }

    public void setList(List<Store.DataEntity.ThemesEntity> list) {
        this.list = list;
    }

    @Override
        public int getItemViewType(int position) {
            if (list.get(position).getType()==1) {
                return TYPE_SINGLE;
            }
            return TYPE_MULTIPLE;
        }

        @Override
        public int getViewTypeCount() {
            return TYPE_COUNT;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            int type = getItemViewType(position);
            if (convertView == null) {
                viewHolder = new ViewHolder();
                if (type == TYPE_SINGLE) {
                    convertView = inflater.inflate(R.layout.item_themes_list, null);
                    viewHolder.title = (TextView) convertView
                            .findViewById(R.id.tv_title);
                    viewHolder.gridView = (GridView) convertView
                            .findViewById(R.id.gridView);
                } else {
                    convertView = inflater.inflate(R.layout.item_themes_list2, null);
                    viewHolder.title = (TextView) convertView
                            .findViewById(R.id.tv_title);
                    viewHolder.gridView = (GridView) convertView
                            .findViewById(R.id.gridView);
                }
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            Store.DataEntity.ThemesEntity themesEntity = list.get(position);
            if (themesEntity != null) {
                if (type == TYPE_SINGLE) {
                    viewHolder.title.setText(themesEntity.getTitle());
                    listItems = themesEntity.getItems();

                    int size = listItems.size();
                    int length = 100;
                    DisplayMetrics dm = new DisplayMetrics();
                    WindowManager wm = (WindowManager) context
                            .getSystemService(Context.WINDOW_SERVICE);
                    wm.getDefaultDisplay().getMetrics(dm);
                    float density = dm.density;
                    int gridviewWidth = (int) (size * (length + 4) * density);
                    int itemWidth = (int) (length * density);

                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                            gridviewWidth, LinearLayout.LayoutParams.MATCH_PARENT);
                    viewHolder.gridView.setLayoutParams(params); // 设置GirdView布局参数,横向布局的关键
                    viewHolder.gridView.setColumnWidth(itemWidth); // 设置列表项宽
                    viewHolder.gridView.setHorizontalSpacing(40); // 设置列表项水平间距
                    viewHolder.gridView.setStretchMode(GridView.NO_STRETCH);
                    viewHolder.gridView.setNumColumns(size); // 设置列数量=列表集合数

                    QuickAdapter<Store.DataEntity.ThemesEntity.ItemsEntity> adapter = new QuickAdapter<Store.DataEntity.ThemesEntity.ItemsEntity>(context, R.layout.list_themes_item, listItems) {
                        @Override
                        protected void convert(BaseAdapterHelper helper, Store.DataEntity.ThemesEntity.ItemsEntity item) {
                            helper.setText(R.id.tv_top_name, item.getTop_name());
                            helper.setImageUrl(R.id.iv_img, item.getImg());
                            helper.setText(R.id.tv_price, item.getPrice());
                        }
                    };
                    viewHolder.gridView.setAdapter(adapter);

                } else {
                    viewHolder.title.setText(themesEntity.getTitle());
                    try {
                        JSONObject jsonObj = new JSONObject(result);
                        JSONObject data = jsonObj.getJSONObject("data");
                        JSONArray themes = data.getJSONArray("themes");
//                        for (int i = 1; i < themes.length(); i++) {
                            JSONObject temp = themes.getJSONObject(position);
                            JSONArray items = temp.getJSONArray("items");
                            listItems2.clear();
                            for (int j = 0; j < items.length(); j++) {
                                JSONObject temp2 = items.getJSONObject(j);
                                String icon = temp2.getString("icon");
                                String title = temp2.getString("title");
                                String sub_title = temp2.getString("sub_title");
                                String price = temp2.getString("price");
                                String price_suffix = temp2.getString("price_suffix");
                                String url = temp2.getString("url");
                                ItemsEntity2 itemsEntity2 = new ItemsEntity2(icon, title, sub_title, price, price_suffix, url);
                                listItems2.add(itemsEntity2);
//                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }finally {
                        viewHolder.gridView.setNumColumns(3);
                        QuickAdapter<ItemsEntity2> adapter = new QuickAdapter<ItemsEntity2>(context, R.layout.list_item_salesmdds, listItems2) {
                            @Override
                            protected void convert(BaseAdapterHelper helper, ItemsEntity2 item) {
                                helper.setText(R.id.tv_salesmdds_where, item.getTitle());
                                helper.setText(R.id.tv_salesmdds_time, item.getSub_title());
                                helper.setText(R.id.tv_salesmdds_money, item.getPrice());
                                helper.setText(R.id.tv, item.getPrice_suffix());
                                helper.setImageUrl(R.id.iv_salesmdds, item.getIcon());
                            }
                        };
                        viewHolder.gridView.setAdapter(adapter);
                    }
                }
            }
            return convertView;
        }

    public void setString(String result) {
        this.result=result;
    }
    private class ViewHolder {
            private TextView title;
            private GridView gridView;
        }
    }